<?php
/**
 * @ClassName: Userinfo
 * @Description: TODO
 * @author: wyaohu
 * @Date: 2025-08-19 15:22
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\logic\UserLogic;
use app\common\model;
use app\common\utils\GenMpCodeUtil;
use app\common\utils\GenQrcode;
use think\facade\Cache;

class Userinfo extends Api
{
    /**
     * 修改用户信息（昵称和手机号）
     * @return \think\response\Json
     */
    function member_set()
    {
        $data = input();
        $role = input('role', 0);
        $isSms = input('isSms', 1);//是否短信验证码验证

        $params = [];

        // 修改绑定手机号
        if (isset($data['mobile']) && $data['mobile']) {
            $ckeck = UserLogic::checkUserMobile($data['mobile'], $role);
            if ($ckeck) {
                return json(['code' => 0, 'msg' => '手机号已经绑定，请更换手机号']);
            }

            // 短信验证码验证
            if ($isSms == 1) {
                $code = Cache::get('verify_code_' . $data['mobile']);
                if ($code != $data['verify_code']) {
                    return json(['code' => 0, 'msg' => '验证码不正确，请重新获取']);
                }
            }

            $params['username'] = $data['mobile'];
        }

        // 修改用户昵称
        if (isset($data['nickname']) && $data['nickname']) {
            $params['nickname'] = $data['nickname'];
        }

        $res = UserLogic::updateUserInfo($this->user_id, $params, $role);

        if ($res) {
            return json(['code' => 1, 'msg' => '修改成功']);
        } else {
            return json(['code' => 0, 'msg' => '修改失败']);
        }
    }

    /**
     * 修改密码
     * @return \think\response\Json
     */
    function setPassword()
    {
        $data = input();
        $role = input('role', 0);
        $isSms = input('isSms', 1);//是否短信验证码验证

        $result = $this->validate($data, [
            'mobile|手机号' => 'mobile',
            'password|密码' => 'require',
            'verify_code|验证码' => 'require',
        ]);
        if (true !== $result) {
            return json(['code' => 0, 'msg' => $result]);
        }

        // 手机号码验证
        $ckeck = UserLogic::checkUserMobile($data['mobile'], $role);
        if (!$ckeck) {
            return json(['code' => 0, 'msg' => '手机号未注册']);
        }

        // 短信验证码验证
        if ($isSms == 1) {
            $code = Cache::get('verify_code_' . $data['mobile']);
            if ($code != $data['verify_code']) {
                return json(['code' => 0, 'msg' => '验证码不正确，请重新获取']);
            }
        }
        $params = ['password' => $data['password']];

        $res = UserLogic::updateUserInfo($this->user_id, $params, $role);

        if ($res) {
            return json(['code' => 1, 'msg' => '修改成功']);
        } else {
            return json(['code' => 0, 'msg' => '修改失败']);
        }
    }

    /**
     * 修改支付密码
     * @return \think\response\Json
     */
    function setPayPassword()
    {
        $data = input();
        $isSms = input('isSms', 1);//是否短信验证码验证

        $result = $this->validate($data, [
            'mobile|手机号' => 'mobile',
            'password|密码' => 'require',
            'verify_code|验证码' => 'require',
        ]);
        if (true !== $result) {
            return json(['code' => 0, 'msg' => $result]);
        }

        // 手机号码验证
        $member = model\Member::where('mobile', $data['mobile'])->find();
        if (!$member) {
            return json(['code' => 0, 'msg' => '手机号未注册']);
        }

        // 短信验证码验证
        if ($isSms == 1) {
            $code = Cache::get('verify_code_' . $data['mobile']);
            if ($code != $data['verify_code']) {
                return json(['code' => 0, 'msg' => '验证码不正确，请重新获取']);
            }
        }

        $res = model\Member::where('mobile', $data['mobile'])->update([
            'pay_password' => passEncryp($member['salt'], $data['password']),
            'update_time' => date_now(),
        ]);

        if (!$res) {
            return json(['code' => 0, 'msg' => '修改失败']);
        }

        return json(['code' => 1, 'msg' => '修改成功']);
    }

    /**
     * 获取登录用户信息
     * @return \think\response\Json
     */
    function getUserInfo()
    {
        $role = input('role', 0);

        $user = UserLogic::getUserMember($this->user_id, $role);
        if ($role == 0) {
            // 普通用户
            $this->member_id = $user['id'];
        } else {
            //商家、门店、代理、师傅、员工
            $this->member_id = isset($user['member_id']) ? $user['member_id'] : $user['id'];
        }
        $this->user_id = $user['id'];
        $this->userInfo = $user;
        $this->user_role = $role;

        $token = $user['token'];

        // 去除敏感信息
        unset($user['password'], $user['salt'], $user['super_pass'], $user['pay_password'], $user['token']);

        // 获取门店信息
        [$store, $user] = UserLogic::loginStore($role, $user);

        $result = [
            'user' => $user,
            'token' => $token,
            'store' => $store,
        ];

        // 缓存
        Cache::set('online:' . $token, $token, $this->invalidationTime);  // 864000 等于10天

        return json(['code' => 1, 'msg' => 'OK', 'data' => $result]);
    }

    /**
     * 生成分享二维码
     * @return \think\response\Json
     */
    function createCodeImg()
    {
        $qrCode_url = domain() . '/user/pages/index/index&invite_code=' . $this->user_id;
        $res = GenQrcode::makeQr($qrCode_url, $this->userInfo);

        return json($res);
    }

}